package me.android.library.utils.android;


import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@UtilityClass
public class MediaUtil {

    public void playFromRes( int resId) {
        try {
            MediaPlayer player = MediaPlayer.create(ContextUtil.getAppContext(), resId);
            player.start();
        } catch (Exception e) {
            log.warn("playFromRes error",e);
        }
    }

    public void playFromAssets(String ringName) {
        try (AssetFileDescriptor fd = SystemServiceUtil.getAssetManager().openFd(ringName)) {
            MediaPlayer player = new MediaPlayer();
            player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
            player.setOnCompletionListener(MediaPlayer::release);
            player.setOnPreparedListener(MediaPlayer::start);
            player.prepareAsync();
        } catch (Exception e) {
            log.warn("playFromAssets error",e);
        }

    }
}
